package com.fxp.project.jiuselu.web.business.v1.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fxp.project.jiuselu.web.base.controller.BaseController;
import com.fxp.project.jiuselu.web.business.v1.pojo.UserInfoVo;
import com.fxp.project.jiuselu.web.domain.CommonResult;
import com.fxp.project.jiuselu.web.models.fare.entity.JslUserFareApplied;
import com.fxp.project.jiuselu.web.models.user.entity.JslUser;
import com.fxp.project.jiuselu.web.models.user.entity.JslUserDelay;
import com.fxp.project.jiuselu.web.models.user.service.IJslUserDelayService;
import com.fxp.project.jiuselu.web.models.user.service.IJslUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.chrono.ChronoLocalDateTime;
import java.util.Date;
import java.util.Optional;

/**
 * ClassName:UserFareController<br/>
 * Function: 用户发票设置<br/>
 * Date:2021年11月22日 11:03<br/>
 *
 * @author Kai.Wang
 * @version 1.0.0
 * @see
 * @since JDK 8
 */
@RestController
@RequestMapping("/v1/user/delay")
public class UserDelayController extends BaseController {
    @Autowired
    private IJslUserDelayService iJslUserDelayService;
    @Autowired
    private IJslUserService jslUserService;

    @PostMapping("seve")
    @CrossOrigin
    public CommonResult setting(@RequestBody @Validated JslUserDelay jslUserDelay) {
        JslUser byId = jslUserService.getById(jslUserDelay.getUserId());
        LocalDateTime localDateTime = byId.getCreateTime().minusMonths(-1);
//        LocalDateTime localDateTime = byId.getCreateTime().plusMonths(1);
//        System.out.println(localDateTime);
        LocalDateTime nowDateTime = LocalDateTime.now();
//        LocalDateTime time = nowDateTime.plusMonths(1);
//        System.out.println(time);
//        userInfoVoByUserId.getCreateTime();
        JslUserDelay delay = iJslUserDelayService.getOne(new QueryWrapper<JslUserDelay>().eq("user_id", jslUserDelay.getUserId()));
//        if (!Optional.ofNullable(delay).isPresent()) {
//            if (nowDateTime.isBefore(localDateTime)) {
//                jslUserDelay.setCreateTime(nowDateTime);
//                jslUserDelay.setCreateBy(super.getCurrentUserInfo().getNickName());
//                iJslUserDelayService.save(jslUserDelay);
//                return CommonResult.success(jslUserDelay);
//            } else return CommonResult.failed("已超时");
//        } else {
//            return CommonResult.success("申请已提交");
//        }
        if(!Optional.ofNullable(delay).isPresent()){
            if(nowDateTime.isBefore(localDateTime)){
                return CommonResult.failed("已超时");
            }else {
                jslUserDelay.setCreateTime(nowDateTime);
                jslUserDelay.setCreateBy(super.getCurrentUserInfo().getNickName());
                iJslUserDelayService.save(jslUserDelay);
                return CommonResult.success("申请已提交");
            }
        }else {
            return CommonResult.failed("已申请过延期,请勿重复提交");
        }
    }
}
